package it.uniroma2.dtk.op.convolution;

import it.uniroma2.util.vector.VectorComposer;

import java.util.Arrays;

public class ReverseCircularConvolution extends CircularConvolution {
	
	@Override
	public double[] op(double[] x, double[] y) {
		if (x == null)
			return y;
		else if (y == null)
			return x;
		if (fft == null)
			return reverseConvolutionBasic(x, y);
		else
			return reverseConvolutionFFT(x, y);
	}
	
	public double[] reverseConvolutionBasic(double[] firstVector, double[] secondVector) {
		int size = firstVector.length;
		double[] result = new double[size];
		Arrays.fill(result, 0);
		for (int i=0; i<size; i++)
			for (int j=0; j<size; j++)
				result[i] += firstVector[j] * secondVector[(i-j)<0 ? size-1-(i-j+size) : size-1-(i-j)];
		return result;
	}
	
	public double[] reverseConvolutionFFT(double[] firstVector, double[] secondVector) {
		return circularConvolutionFFT(VectorComposer.reverse(firstVector), VectorComposer.reverse(secondVector));
	}

}
